package net.steam.easyjoboa.dao;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import net.steam.easyjoboa.entity.Employee;
import net.steam.easyjoboa.entity.Menu;
import net.steam.easyjoboa.entity.Roles;
import net.steam.easyjoboa.web.action.MenuAction;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class MenuDao extends HibernateDaoSupport{
	
	@SuppressWarnings("unchecked")
	public List<Menu> findMenuByRole(Integer userId,String atid) {
		Employee user = getHibernateTemplate().get(Employee.class, userId);
		List<Roles> roles = user.getRoleses();
		Set<Menu> menus = new HashSet<Menu>();
		for (Roles role : roles) {
			menus.addAll(role.getMenus());
		}
		List<Menu> list = new ArrayList<Menu>();
		int parentId = 0;
		try {
			parentId = Integer.parseInt(atid);
		} catch (Exception e){}
		for (Menu menu : menus) {
			if (menu.getParentId() == parentId) {
				list.add(menu);
			}
		}
		return list;
	}
	
	public List<Menu> findMenuByRole2(Integer userId) {
		Employee user = getHibernateTemplate().get(Employee.class, userId);
		List<Roles> roles = user.getRoleses();
		Set<Menu> menus = new HashSet<Menu>();
		for (Roles role : roles) {
			menus.addAll(role.getMenus());
		}
		List<Menu> list = new ArrayList<Menu>();
		int parentId = 0;
		for (Menu menu : menus) {
				list.add(menu);
		}
		return list;
	}
}
